package com.wrench.elevator.base.user.service;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.wrench.elevator.base.user.dao.RechargeDao;
import com.wrench.elevator.base.user.domain.Recharge;
import com.wrench.elevator.constant.Dict;
import com.wrench.elevator.constant.Result;
import com.wrench.elevator.exception.WrenchException;
import com.wrench.elevator.util.WrenchUtil;

@Component
public class RechargeService
{
    @Resource
    private RechargeDao dao;
    
    @Resource
    private WalletService walletService;
    
    @Resource
    private FlowService flowService;
    
    private static final Logger logger = LoggerFactory.getLogger(RechargeService.class);
    
    @Transactional(rollbackFor = WrenchException.class)
    public int addRecharge(String money, String user_id)
        throws WrenchException
    {
        // 判空
        if (WrenchUtil.hasNull(money, user_id))
        {
            logger.error("==> param is not found , money = {} , user_id = {} ", money, user_id);
            throw new WrenchException(Result.PARAM_NOT_FOUND_CODE, Result.PARAM_NOT_FOUND_MESSAGE);
        }
        
        // 判断user_id是不是整数
        if (!WrenchUtil.isInt(user_id))
        {
            
            logger.error("==> string is not int , user_id = {} ", user_id);
            throw new WrenchException(Result.STRING_IS_NOT_INT_CODE, Result.STRING_IS_NOT_INT_MESSAGE);
            
        }
        // 判断money是不是金额的格式 整数或在两位以下小数
        
        if (!WrenchUtil.isMoneyStr(money))
        {
            logger.error("==> string is not money  , money = {} ", money);
            throw new WrenchException(Result.STRING_IS_NOT_MONEY_CODE, Result.STRING_IS_NOT_MONEY_MESSAGE);
        }
        
        walletService.recharge(money, user_id);
        
        String flow_type = Dict.FLOW_NO_HEAD_CZ;
        String flow_no = WrenchUtil.getFlowNo(flow_type);
        String flow_time = WrenchUtil.nowTime();
        
        flowService.addFlow(flow_type, money, user_id, flow_no, flow_time);
        
        Recharge recharge = new Recharge();
        recharge.setRecharge_flow_no(flow_no);
        recharge.setRecharge_money(Double.valueOf(money));
        recharge.setRecharge_time(flow_time);
        recharge.setUser_id(Integer.valueOf(user_id));
        recharge.setRecharge_status(Dict.RECHARGE_STATUS_ING);
        
        int commit = -1;
        
        try
        {
            commit = dao.addRecharge(recharge);
        }
        catch (Exception e)
        {
            logger.error("==>  commit= dao.addRecharge(recharge) execute error , user_id = {} , money = {} , error = {} ",
                user_id,
                money,
                e);
            throw new WrenchException(Result.DB_INSERT_ERROR_CODE, Result.DB_INSERT_ERROR_MESSAGE);
        }
        
        if (commit <= 0)
        {
            logger.error("==> commit= dao.addRecharge(recharge) execute success , but commit <=0 , commit = {} , user_id = {} , money = {}  ",
                commit,
                user_id,
                money);
            throw new WrenchException(Result.RECHARGE_ADD_RECHARGE_FAIL_CODE, Result.RECHARGE_ADD_RECHARGE_FAIL_MESSAGE);
        }
        
        logger.debug("==> commit= dao.addRecharge(recharge) execute success , user_id = {} , money = {} , commit = {} ",
            user_id,
            money,
            commit);
        
        return commit;
    }
    
}
